<template>
  <div class="business-satisfaction-query-container">
    <vab-query-form>
      <el-form>
        <vab-query-form-left-panel :span="12">
          <el-form-item class="form-item">
            <el-cascader
              v-model="queryForm.cityArea"
              :options="cityOptions"
              :props="citySelect"
              placeholder="选择地区"
              clearable
            />
          </el-form-item>
          <el-form-item class="form-item">
            <el-cascader
              v-model="queryForm.industry"
              :options="industryOptions"
              :props="industrySelect"
              placeholder="选择行业"
              clearable
            />
          </el-form-item>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.searchKey"
              clearable
              placeholder="请输入客户名称或合同编号"
            />
          </el-form-item>
        </vab-query-form-left-panel>
        <vab-query-form-right-panel :span="12">
          <el-form-item>
            <el-cascader
              v-model="queryForm.industry"
              :options="industryOptions"
              :props="industrySelect"
              placeholder="选择产品"
              clearable
            />
          </el-form-item>
          <el-form-item label="合同属性">
            <el-select v-model="queryForm.orderType">
              <el-option label="新签" />
            </el-select>
          </el-form-item>
          <el-form-item label="服务周期">
            <DateSelect ref="DateSelectRef" @get-date="getDate" />
          </el-form-item>
        </vab-query-form-right-panel>
        <vab-query-form-left-panel :span="12">
          <el-form-item label="小记类型">
            <el-select>
              <el-option label="跟进小记" />
              <el-option label="服务小记" />
              <el-option label="客满小记" />
              <el-option label="店铺后台数据" />
            </el-select>
          </el-form-item>
          <el-form-item label="有没有写小记">
            <el-select>
              <el-option label="没写小记的" />
              <el-option label="写了小记的" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select placeholder="流程进度">
              <el-option />
            </el-select>
          </el-form-item>
        </vab-query-form-left-panel>
      </el-form>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      @selection-change="setSelectRows"
    >
      <el-table-column align="center" show-overflow-tooltip type="selection" />
      <el-table-column
        align="center"
        label="id"
        prop="id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="标题"
        prop="title"
        show-overflow-tooltip
      />
      <el-table-column align="center" label="操作" width="200">
        <template #default="{ row }">
          <el-button text @click="handleEdit(row)">编辑</el-button>
          <el-button text @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="editRef" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { getList, doDelete } from '@/api/businessSatisfactionQuery'
  import Edit from './components/BusinessSatisfactionQueryEdit'
  import DateSelect from '/src/components/DateSelect.vue'
  import { Delete, Plus, Search } from '@element-plus/icons-vue'

  export default defineComponent({
    name: 'BusinessSatisfactionQuery',
    components: { Edit, DateSelect },
    setup() {
      const $baseConfirm = inject('$baseConfirm')
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        editRef: null,
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNo: 1,
          pageSize: 10,
          title: '',
        },
      })

      const setSelectRows = (val) => {
        state.selectRows = val
      }
      const handleEdit = (row) => {
        if (row.id) {
          state.editRef.showEdit(row)
        } else {
          state.editRef.showEdit()
        }
      }
      const handleDelete = (row) => {
        if (row.id) {
          $baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await doDelete({ ids: row.id })
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            await fetchData()
          })
        } else {
          if (state.selectRows.length > 0) {
            const ids = state.selectRows.map((item) => item.id).join()
            $baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await doDelete({ ids })
              $baseMessage(msg, 'success', 'vab-hey-message-success')
              await fetchData()
            })
          } else {
            $baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      }
      const handleSizeChange = (val) => {
        state.queryForm.pageSize = val
        fetchData()
      }
      const handleCurrentChange = (val) => {
        state.queryForm.pageNo = val
        fetchData()
      }
      const queryData = () => {
        state.queryForm.pageNo = 1
        fetchData()
      }
      const fetchData = async () => {
        state.listLoading = true
        const {
          data: { list, total },
        } = await getList(state.queryForm)
        state.list = list
        state.total = total
        state.listLoading = false
      }
      onMounted(() => {
        // fetchData()
      })

      return {
        ...toRefs(state),
        setSelectRows,
        handleEdit,
        handleDelete,
        handleSizeChange,
        handleCurrentChange,
        queryData,
        fetchData,
        Delete,
        Plus,
        Search,
      }
    },
  })
</script>

<style>
  .form-item {
    margin: 0 20px;
  }
</style>
